﻿using System;
using CodeArt.SharePoint.CamlQuery;
using Microsoft.SharePoint;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject2010
{
    [TestClass]
    public class UnitTest2010
    {
        private TestContext testContextInstance;
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }

        [TestMethod]
        public void TestInExpre()
        {
            QueryField f = new QueryField("Title", true);
            
            var site = new SPSite("http://dc:83/sites/db");
            var web = site.RootWeb;
            var list = web.Lists["月度计划"];

            var items = ListQuery.From(list, QueryScope.RecursiveAll).Where(f.In("1111", "2222")).GetItems();

            Assert.IsTrue(items.Count >= 2);
        }

        [TestMethod]
        public void TestInExpre2()
        {
            TypedQueryField<int> f = new TypedQueryField<int>("ID", true);

            var site = new SPSite("http://dc:83/sites/db");
            var web = site.RootWeb;
            var list = web.Lists["月度计划"];

            var items = ListQuery.From(list, QueryScope.RecursiveAll).Where(f.In(32,789)).GetItems();

            Assert.IsTrue(items.Count >= 1);
        }

        [TestMethod]
        public void TestInExpre3()
        {
            LookupQueryField f = new LookupQueryField("关联年度计划", false);
           // TypedQueryField<int> f = new TypedQueryField<int>("关联年度计划", false);

            var site = new SPSite("http://dc:83/sites/db");
            var web = site.RootWeb;
            var list = web.Lists["月度计划"];

            var items = ListQuery.From(list, QueryScope.RecursiveAll).Where(f.In(6,33)).GetItems();

            Assert.IsTrue(items.Count >= 1);
        }

        [TestMethod]
        public void TestMembershipField()
        {
            MembershipQueryField f = new MembershipQueryField("部门领导", false);

            var site = new SPSite("http://dc:83/sites/db");
            var web = site.RootWeb;
            var list = web.Lists["月度计划"];

            var grp = web.SiteGroups["财务会计部"];

            var items = ListQuery.From(list, QueryScope.RecursiveAll).Where(f.UserInGroup(grp.ID)).GetItems();

            Assert.IsTrue(items.Count >= 1);
        }

        [TestMethod]
        public void TestMembershipField2()
        {
            MembershipQueryField f = new MembershipQueryField("部门领导", false);

            var site = new SPSite("http://dc:83/sites/db");
            var web = site.RootWeb;
            var list = web.Lists["月度计划"];

            var grp = web.SiteGroups["所有人员"];

            var items = ListQuery.From(list, QueryScope.RecursiveAll).Where(f.ContainsCurrentUser()).GetItems();

            Assert.IsTrue(items.Count >= 1);
        }

        [TestMethod]
        public void TestInclude()
        {
            LookupQueryField f = new LookupQueryField("AssignedTo", true);

            var site = new SPSite("http://chshavdms01:93/");
            var web = site.RootWeb;
            var list = web.Lists["Task01"];

            var items = ListQuery.From(list, QueryScope.RecursiveAll).Where(f.Includes("测试1")).GetItems();
            Assert.IsTrue(items.Count == 1);

            var items2 = ListQuery.From(list, QueryScope.RecursiveAll).Where(f.Includes(6)).GetItems();
            Assert.IsTrue(items2.Count == 1);
        }

        [TestMethod]
        public void TestNotInclude()
        {
            LookupQueryField f = new LookupQueryField("AssignedTo", true);

            var site = new SPSite("http://chshavdms01:93/");
            var web = site.RootWeb;
            var list = web.Lists["Task01"];

            var items = ListQuery.From(list, QueryScope.RecursiveAll).Where(f.NotIncludes("测试1")).GetItems();
            Assert.IsTrue(items.Count == 1);

            TestContext.WriteLine(items[0].Title);

            var items2 = ListQuery.From(list, QueryScope.RecursiveAll).Where(f.NotIncludes(61)).GetItems();
               
            Assert.IsTrue(items2.Count == 1);
        }

        [TestMethod]
        public void TestJoin()
        {
            LookupQueryField f = new LookupQueryField("UserC", true);
            QueryField titleField = new QueryField("Title",true);
            QueryField fullNameField = new QueryField("UserFullName", true);

            var site = new SPSite("http://chshavdms01:93/");
            var web = site.RootWeb;
            var list = web.Lists["任务"];

            var lq = ListQuery.From(list, QueryScope.RecursiveAll)
                .Join(JoinType.LEFT, "UserC").As("user").Project("Title", "UserTitle").Project("FullName","UserFullName")
                .Where(fullNameField.Contains("222"));            

            var spQuery = lq.GetQuery();

            var items2 = list.GetItems(spQuery);

             var xml = spQuery.ViewXml;

            TestContext.WriteLine(items2.Count.ToString());
            TestContext.WriteLine(items2[0]["UserFullName"].ToString());

            var items = lq.GetItems();
            Assert.IsTrue(items.Count == 1);          
        }

        [TestMethod]
        public void TestJoin2()
        {
            var site = new SPSite("http://chshavdms01:93/");
            var web = site.RootWeb;
            var list = web.Lists["任务"];

            LookupQueryField f = new LookupQueryField("UserC", true);
            QueryField titleField = new QueryField("Title", true);
            QueryField fullNameField = new QueryField("UserFullName", true);            

            var lq = ListQuery.Select("calLocation", "UserFullName")
                .From(list, QueryScope.RecursiveAll)
                .Join(JoinType.LEFT, "UserC").As("user").Project("Title", "UserTitle").Project("FullName", "UserFullName")
                .Join(JoinType.LEFT,"user","calendar01").As("cal").Project("Location","calLocation")   
                .Where(fullNameField.Contains("22"));

            var spQuery = lq.GetQuery();

            var items2 = list.GetItems(spQuery);

            var xml = spQuery.ViewXml;
            TestContext.WriteLine(xml);

            TestContext.WriteLine(items2.Count.ToString());
            TestContext.WriteLine(items2[0]["UserFullName"].ToString());
            TestContext.WriteLine(items2[0]["calLocation"].ToString());

            var items = lq.GetItems();
            Assert.IsTrue(items.Count == 1);
        }
    }
}
